package com.kws.commercial.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.commercial.dao.CommercialSchemeQuotationDetails;
import com.kws.commercial.dao.CommercialSingleQuotation;
import com.kws.commercial.dao.Rq.CreateQuotationDetailsRq;
import com.kws.commercial.mapper.CommercialSchemeQuotationMapper;
import com.kws.commercial.service.CommercialSchemeQuotationService;
import com.kws.commercial.service.CommercialSingleQuotationService;
import com.kws.common.core.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <b>CommercialSchemeQuotationServiceImpl</b>
 *
 * @description: CommercialSchemeQuotationServiceImpl <br>
 * @date: 2024/10/28 09:28 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class CommercialSchemeQuotationServiceImpl extends ServiceImpl<CommercialSchemeQuotationMapper, CommercialSchemeQuotationDetails> implements CommercialSchemeQuotationService {
    private static final Logger logger = LoggerFactory.getLogger(CommercialSchemeQuotationServiceImpl.class);

    @Autowired
    private CommercialSingleQuotationService commercialSingleQuotationService;

    @Override
    @Transactional
    public boolean createQuotationDetails(CreateQuotationDetailsRq rq) {

        if (CollectionUtil.isNotEmpty(rq.getSchemeQuotationDetailsList())){
            rq.getSchemeQuotationDetailsList().forEach(item -> {
                CommercialSchemeQuotationDetails commercialSchemeQuotationDetails = new CommercialSchemeQuotationDetails();
                commercialSchemeQuotationDetails.setProjectQuotationId(rq.getId());
                commercialSchemeQuotationDetails.setSchemeName(item.getSchemeName());
                commercialSchemeQuotationDetails.setSchemePrice(item.getSchemePrice());
                int insert = baseMapper.insert(commercialSchemeQuotationDetails);
                if (insert == 1 && CollectionUtil.isNotEmpty(item.getSingleQuotationList())){
                    List<CommercialSingleQuotation> list = new ArrayList<>();
                    item.getSingleQuotationList().forEach(value -> {
                        CommercialSingleQuotation commercialSingleQuotation = new CommercialSingleQuotation();
                        commercialSingleQuotation.setQuotationDetailsId(commercialSchemeQuotationDetails.getId());
                        commercialSingleQuotation.setProjectName(value.getProjectName());
                        commercialSingleQuotation.setProjectDescription(value.getProjectDescription());
                        commercialSingleQuotation.setItemizedPrice(value.getItemizedPrice());
                        commercialSingleQuotation.setAccessoryUrl(value.getAccessoryUrl());
                        list.add(commercialSingleQuotation);
                    });
                    boolean b = commercialSingleQuotationService.saveBatch(list);
                }else if (insert == 0){
                    throw new ServiceException("保存项目报价失败，请重试！");
                }
            });
        }
        return true;
    }
}
